<template>
  <div class="comment-input">
    <div class="disable" v-show="!isActive">
      <!-- 这里是未激活状态 -->
      <input type="text" placeholder="写跟帖" @click="handleClickInput" />
      <div class="commentWrapper">
        <span class="iconfont iconpinglun-"></span>
        <div class="number">1020</div>
      </div>
      <span class="iconfont iconshoucang"></span>
      <span class="iconfont iconfenxiang"></span>
    </div>
    <div class="enable" v-show="isActive">
      <!-- 这里是已激活状态 -->
      <textarea
        rows="3"
        ref="commentTextarea"
        @blur="handleTextareaBlur"
        v-model="comment"
      ></textarea>
      <div class="btnSubmit" @click="sendComment">发送</div>
    </div>
  </div>
</template>

<script>
import eventBus from "../../utils/index";
export default {
  name: "CommentInput",
  data() {
    return {
      isActive: false,
      comment: "",
      commentId: "",
    };
  },
  mounted() {
    eventBus.$off("clickReply"); // 确保把上一次的事件清除掉
    eventBus.$on("clickReply", (id) => {
      this.handleClickInput();
      this.commentId = id;
    });
  },
  methods: {
    handleClickInput(id) {
      this.isActive = true;
      this.$nextTick(() => {
        this.$refs.commentTextarea.focus();
      });
      this.$emit("isActive");
    },
    sendComment() {
      this.$axios
        .post("/post_comment/" + this.$route.query.id, {
          content: this.comment,
          parent_id: this.commentId,
        })
        .then((res) => {
          this.comment = "";
          this.$emit("sendComment");
        });
    },
    handleTextareaBlur() {
      // 这里设置延迟定时器的目的解决用户无法点击发送按钮，因为blur事件比click事件先触发，所以失去焦点后让输入框马上消失会导致点击事件失效
      setTimeout(() => {
        this.isActive = false;
      }, 50);
    },
  },
};
</script>

<style scoped lang="less">
.comment-input {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: #f2f2f2;
  .iconfont {
    font-size: 24/360 * 100vw;
  }
  .disable {
    display: flex;
    justify-content: space-around;
    padding: 10/360 * 100vw 0;
    input {
      background: #d7d7d7;
      font-size: 14/360 * 100vw;
      line-height: 30/360 * 100vw;
      border-radius: 16/360 * 100vw;
      border: none;
      outline: none;
      padding: 0 16/360 * 100vw;
      width: 176/360 * 100vw;
      box-sizing: border-box;
    }
    .commentWrapper {
      position: relative;
      .number {
        position: absolute;
        top: -5px;
        left: 4px;
        background: red;
        color: #fff;
        font-size: 12/360 * 100vw;
        line-height: 16/360 * 100vw;
        border-radius: 10/360 * 100vw;
        padding: 0 6/360 * 100vw;
      }
    }
  }
  .enable {
    padding: 10/360 * 100vw;
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    textarea {
      width: 260/360 * 100vw;
      background: #d7d7d7;
      border: none;
      outline: none;
      border-radius: 8/360 * 100vw;
      padding: 8/360 * 100vw;
      box-sizing: border-box;
      font-size: 16/360 * 100vw;
      resize: none;
    }
    .btnSubmit {
      background: red;
      color: #fff;
      font-size: 14/360 * 100vw;
      line-height: 26/360 * 100vw;
      border-radius: 14/360 * 100vw;
      padding: 0 14/360 * 100vw;
    }
  }
}
</style>
